package com.muyu.system.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.DataPageResp;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.system.domain.SysDictData;
import com.muyu.system.domain.model.SysDictDataAddModel;
import com.muyu.system.domain.model.SysDictDataPageQueryModel;
import com.muyu.system.domain.model.SysDictDataUpdModel;
import com.muyu.system.domain.req.SysDictDataAddReq;
import com.muyu.system.domain.req.SysDictDataListReq;
import com.muyu.system.domain.req.SysDictDataUpdReq;
import com.muyu.system.domain.resp.SysDictDataListResp;
import com.muyu.system.service.SysDictDataService;
import com.muyu.system.service.SysDictTypeService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据字典信息
 *
 * @author muyu
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController extends BaseController {
    @Autowired
    private SysDictDataService dictDataService;

    @Autowired
    private SysDictTypeService dictTypeService;

    /**
     * 查询字典数据列表
     * @param sysDictDataListReq
     * @return
     */
    @RequiresPermissions("system:dict:list")
    @PostMapping("/list")
    public Result<DataPageResp<SysDictDataListResp>> list (@RequestBody SysDictDataListReq sysDictDataListReq) {
        PageQueryModel<SysDictData> pageQueryModel = dictDataService.selectDictDataList(SysDictDataPageQueryModel.reqBuild(sysDictDataListReq));
        DataPageResp.of(pageQueryModel).cover(SysDictDataListResp::buildResp);
        return Result.success(DataPageResp.of(pageQueryModel).cover(SysDictDataListResp::buildResp));
    }

    /**
     * 导出字典数据列表
     * @param response
     * @param sysDictDataListReq
     */
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    public void export (HttpServletResponse response, SysDictDataListReq sysDictDataListReq) {
        // TODO 需要重写导出查询
        PageQueryModel<SysDictData> list = dictDataService.selectDictDataList(SysDictDataPageQueryModel.reqBuild(sysDictDataListReq));
        ExcelUtil<SysDictData> util = new ExcelUtil<>(SysDictData.class);
        util.exportExcel(response, list.getDataList(), "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictCode}")
    public Result<SysDictData> getInfo (@PathVariable("dictCode") Long dictCode) {
        return success(dictDataService.getOneById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public Result<List<SysDictData>> dictType (@PathVariable("dictType") String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<>();
        }
        return success(data);
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<String> add (@Validated @RequestBody SysDictDataAddReq sysDictDataAddReq) {
        dictDataService.insertDictData(SysDictDataAddModel.buildAddModel(sysDictDataAddReq));
        return Result.success();
    }

    /**
     * 修改保存字典类型
     */
    //@RequiresPermissions("system:dict:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<String> edit (@Validated @RequestBody SysDictDataUpdReq sysDictDataUpdReq) {
        dictDataService.updateDictData(SysDictDataUpdModel.buildUpdModel(sysDictDataUpdReq));
        return Result.success();
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public Result<String> remove (@PathVariable("dictCodes") List<Long> dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
